package net.lateeye.search.googlecom;

import java.util.LinkedList;

import net.lateeye.search.AbsResultSet;
import net.lateeye.search.AbsSearchResultPageTextProcessor;
import net.lateeye.search.SearchResult;

/**
 * Visibility: From the same package only. Classes in other packages must use
 * GoogleComResultpageProcessor.
 * 
 * @author Isao Isaac Saito <130s@lateeye.net>
 * @deprecated 1/15/2011. Google.com accessor is no longer available, due to the
 *             possible conflict with the terms of service of the company. See
 *             http://code.google.com/p/cse-sql-api/wiki/Accessor_GoogleCom
 *             ?ts=1295123079&updated=Accessor_GoogleCom#Alert
 */
class GoogleComResultpageProcessor_2011Jan extends
		AbsSearchResultPageTextProcessor
{

	@Override
	/*
	 * @deprecated Not implemented yet.
	 */
	protected String createResult(String queryStr, int orderInResults,
			String docUrl, String docTitle, String urlDomain, String summary)
	{
		return null;
	}

	@Override
	protected String extractDocTitle(String result)
	{
		String triggeringString = "<a href";
		String prefix_targetString = ">";
		String suffix_targetString = "</a>";
		String str = this.extractSingleString(result, triggeringString,
				prefix_targetString, suffix_targetString);
		return str;
	}

	@Override
	protected String extractDocUrl(String htmlbody_searchResult)
	{
		// 1/3/11 Toggled to see first that the old part is still valid.
		//
		// String triggeringString = "h3 class=\"r\""; // \" stands for double
		// quote.
		// String prefix_targetString = "<a href=\"";
		// String suffix_targetString = " class=l";
		// LinkedList<String> results = super.extractStringList(result_str,
		// prefix_targetString, triggeringString, suffix_targetString);

		// Pattern defined on Apr, 2008
		String triggeringString = "<a href";
		String prefix_targetString = "=\"";
		String suffix_targetString = "\" class";
		String str = this.extractSingleString(htmlbody_searchResult,
				triggeringString, prefix_targetString, suffix_targetString);
		str = str.replaceAll("\"", "");
		return str;
	}

	@Override
	protected long extractEstimatedSize(String htmlbody_searchResult)
	{
		long answer = -1;
		// String trigger_targetString = "<div>";
		String trigger_targetString = "<div id=resultStats";
		// String prefix_targetString = "<div id=resultStats>About ";
		String prefix_targetString = ">About ";
		String suffix_targetString = " results<nobr>  (";
		LinkedList<String> results = super.extractStringList(
				htmlbody_searchResult, prefix_targetString,
				trigger_targetString, suffix_targetString);
		String size = null;
		if (0 < results.size()) {
			size = results.get(0).replaceAll(",", "");
			// TODO need to apply Regex in the future
			size = size.replaceAll(">", "");
			answer = Long.parseLong(size);
		}
		return answer;
	}

	@Override
	protected SearchResult extractOtherMetadata(String result_str,
			SearchResult result_map)
	{
		// TODO 1/3/11/130s) Need processes here to add other metadata.

		return result_map;
	}

	@Override
	protected LinkedList<String> extractResults(String htmlbody_searchResult)
	{
		String triggerOfEachResult = "li class=g";
		String prefix_targetString = "<h3 class=\"r\">";
		String suffix_targetString = "</div>";
		LinkedList<String> results = super.extractStringList(
				htmlbody_searchResult, prefix_targetString,
				triggerOfEachResult, suffix_targetString);
		return results;
	}

	@Override
	protected String extractSummary(String htmlbody_searchResult)
	{
		String triggeringString = "<div class=vsc";
		String prefix_targetString = ">";
		String suffix_targetString = "<br><span class=a>";
		String str = this.extractSingleString(htmlbody_searchResult,
				triggeringString, prefix_targetString, suffix_targetString);
		return str;
	}

	@Override
	protected AbsResultSet instantiateResultSet()
	{
		return new GoogleComResultSet();
	}

	@Override
	protected SearchResult instantiateSingleSearchResult()
	{
		return new GoogleComSearchResult();
	}
}
